---
title: "IV Dashboard Awareness Impact"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
---

```{r, include=FALSE, echo=FALSE, message=FALSE, warning=FALSE}
#Income verification experimentation revenue impact estimate

library(tidyverse)
library(prophet)
library(lubridate)
library(dygraphs)
library(xts)
library(quantmod)
library(flexdashboard)

#ns = as_tibble(read.csv("Income_Users_2022_01_18.csv")) #read in historic data -- use this for income verification experiment impact analysis

#ns = as_tibble(read.csv("Income_Users_2022_02_01.csv")) #read in historic data -- use this for page change impact analysis

ns = as_tibble(read.csv("Income_Users_Daily_2022_04_01.csv")) #read in data - should be updated to read directly from Cupola query

ns$Day = as.Date(ns$Day)

##prophet model
z = ns %>% filter(Day >= "2021-02-19") %>% select(Day,Total) %>% rename(ds = 1, y = 2)

z = z %>% filter(ds < "2022-03-07") #day IV awareness dash go live

#code from previous LLI NE forecasting efforts -- modified for RI seasonal trends 
z$sat = ifelse(weekdays(as.Date(z$ds)) == 'Saturday',1,0)
z$sun = ifelse(weekdays(as.Date(z$ds)) == 'Sunday',1,0)
z$m1 = ifelse(day(z$ds) == 1,1,0) #add binary dummy variable for first of the month
z$m2 = ifelse(day(z$ds) == 2,1,0) #add binary dummy variable for second of the month
z$m31 = ifelse(day(z$ds) == 31,1,0) #add binary dummy variable for 31st of the month
z$m30 = ifelse(day(z$ds) == 30,1,0) #add binary dummy variable for 30th of the month


m <- prophet(daily.seasonality = 50, changepoint.prior.scale = 0.5, seasonality.prior.scale = 0.1)
m <- add_regressor(m,'m1')
m <- add_regressor(m,'m2')
m <- add_regressor(m,'m31')
m <- add_regressor(m,'m30')
m <- add_regressor(m,'sat')
m <- add_regressor(m,'sun')
m  = add_country_holidays(m, 'US')
m <- add_seasonality(m, name='monthly', period=30.5, fourier.order=5)
m <- fit.prophet(m, z)

future <- make_future_dataframe(m, periods = 28)
future$sat = ifelse(weekdays(as.Date(future$ds)) == 'Saturday',1,0)
future$sun = ifelse(weekdays(as.Date(future$ds)) == 'Sunday',1,0)
future$m1 = ifelse(day(future$ds) == 1,1,0) #add binary dummy variable for first of the month
future$m2 = ifelse(day(future$ds) == 2,1,0) #add binary dummy variable for second of the month
future$m31 = ifelse(day(future$ds) == 31,1,0) #add binary dummy variable for 31st of the month
future$m30 = ifelse(day(future$ds) == 30,1,0) #add binary dummy variable for 30th of the month

forecast <- predict(m, future)
prophet_plot_components(m, forecast) ##use these to create plots
dyplot.prophet(m, forecast)

ns$Total[272] - round(forecast$yhat[283],0) #14 fewer IV enabled customers than expected on 3/31

ns$Total[272] - round(forecast$yhat_upper[283],0) #67 fewer IV enabled customers than expected on 3/31

ns$Total[272] - round(forecast$yhat_lower[283],0) #37 more IV enabled customers than expected on 3/31

#create xts object for plotting
z1 = ns %>% filter(Day >= "2021-02-19") %>% select(Day,Total) %>% rename(ds = 1, y = 2)
z3 = forecast %>% select(ds, yhat, yhat_lower, yhat_upper)

z2 = left_join(z1,z3)

z4 = xts(x = z2, order.by = z2$ds)

```
```{r}
dygraph(z4) %>% dySeries(c("yhat_lower", "yhat", "yhat_upper"), label = "forecast") %>% dySeries("y", label = "actual") %>% dyOptions(colors = RColorBrewer::brewer.pal(3, "Set1")) %>% dyLegend(show = "onmouseover") %>% dyRangeSelector() %>% dyEvent("2022-03-07", "IV Dashboard Banner Go-Live", labelLoc = "bottom")
```